#include "threadPool.h"
#include <crypt.h>

void getSalt(char *salt, char *pwd){
    int i, j;
    for(i = 0, j = 0; pwd[i] && j != 3; ++i){
        if(pwd[i] == '$'){
            ++j;
        }
    }
    strncpy(salt, pwd, i - 1);
}

int pwdCheck(char *user, char *pwd){
    struct spwd *sp;
    char salt[512] = {0};
    
    if((sp = getspnam(user)) == NULL){
        perror("getting username and password");
        exit(-2);
    }
    getSalt(salt, sp->sp_pwdp);

    if(strcmp(sp->sp_pwdp, crypt(pwd, salt)) == 0){
        // succeed
        return 0;
    }
    else{
        // failed
        return -1;
    }
}
